﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;

namespace Clannan.WebService
{
    /// <summary>
    /// Summary description for PicUpload
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PicUpload : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        public string SaveImage(string imageStreamString,string imageName)//保存上传图片
        {
            var imageFilePath = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "/UploadFile/Images/" + imageName + "";
            string result = SaveImageFun(imageStreamString, imageFilePath);
            if (result == "1")
            {
                Session["success"] = "1";
                return "http://" + this.Context.Request.Url.Host + ":" + this.Context.Request.Url.Port + "/UploadFile/Images/" + imageName + "";
            }
            else
            {
                return result;
            }
        }

        [WebMethod(EnableSession = true)]
        public string SaveHeaderImage(string imageStreamString,string imageName)//保存头像图片
        {
            var binaryData = Convert.FromBase64String(imageStreamString);
            var imageFilePath = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "/UploadFile/HeadImage/" + imageName + "";
            if (File.Exists(imageFilePath))
            {
                File.Delete(imageFilePath);
            }
            var stream = new System.IO.FileStream(imageFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            try
            {
                stream.Write(binaryData, 0, binaryData.Length);
                Session["success"] = "1";
                return "1";//保存图片成功
            }
            catch (Exception ex)
            {
                return "0";//保存图片失败
            }
            finally
            {
                stream.Close();
            }
        }

        [WebMethod]
        public string SaveMobileImage(string imageStreamString,string imageName)//保存手机上传图片
        {
            var imageFilePath = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "/UploadFile/MobileImages/" + imageName + "";
            string result = SaveImageFun(imageStreamString, imageFilePath);
            if (result == "1")
            {
                return "http://" + this.Context.Request.Url.Host + ":" + this.Context.Request.Url.Port + "/UploadFile/MobileImages/" + imageName + "";
            }
            else
            {
                return "0";//失败返回0
            }
        }
    

        /// <summary>
        /// 保存图片方法
        /// </summary>
        /// <param name="imageStreamString">图片流</param>
        /// <param name="imageFilePath">存放的图片路径</param>
        /// <returns></returns>
        private string SaveImageFun(string imageStreamString,string imageFilePath)
        {
            var binaryData = Convert.FromBase64String(imageStreamString);
            if (File.Exists(imageFilePath))
            {
                File.Delete(imageFilePath);
            }
            var stream = new System.IO.FileStream(imageFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            try
            {
                stream.Write(binaryData, 0, binaryData.Length);
                return "1";//保存图片成功
            }
            catch (Exception ex)
            {
                return "0";//保存图片失败
            }
            finally
            {
                stream.Close();
            }
        }

        private FileStream stream = null;
        private string imageFilePath = string.Empty;

        [WebMethod]
        public int SaveWavHeader(byte[] wavFileHeader, byte[] readByte,string name)//保存音频
        {
            int read = 0;
            byte[] buffer = new byte[4096];
            try
            {
                imageFilePath = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "/UploadFile/Voice/" + name + "";
                if (File.Exists(imageFilePath))
                {
                    File.Delete(imageFilePath);
                }
                stream = new System.IO.FileStream(imageFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                stream.Write(wavFileHeader, 0, wavFileHeader.Length);
                MemoryStream str = new MemoryStream(readByte);
                while ((read = str.Read(buffer, 0, buffer.Length)) > 0)
                {
                    stream.Write(buffer, 0, read);
                }
                return 1;//保存成功
            }
            catch (Exception ex)
            {
                return 0;//保存失败
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }
    }
}
